package com.cepri.service.yingxiao.baidutts;

import android.content.Context;

import com.cepri.service.yingxiao.ITTSListener;

/**
 * 描述：百度语音合成管理类
 * 作者：jianiuqi 2020-10-07 16:42
 * 修改描述：增加回调接口，优化逻辑
 * 修 改 人：小卷毛
 * 修改版本：v1.2.1
 */
public class BaiduTtsManager implements ITtsManager {

    private NonBlockSyntherizer mSyntherizer;

    private static class DtsManagerHolder {
        static final BaiduTtsManager INSTANCE = new BaiduTtsManager();
    }

    /**
     * 获取单例对象
     */
    public static BaiduTtsManager getInstance() {
        return BaiduTtsManager.DtsManagerHolder.INSTANCE;
    }

    // 私有化构造函数
    private BaiduTtsManager() {}

    /**
     * 初始化语音合成
     */
    @Override
    public void init(Context context) {
        if (mSyntherizer == null)
            mSyntherizer = new NonBlockSyntherizer(context.getApplicationContext());
    }

    /**
     * 重新初始化语音合成
     */
    public void initNew(Context context) {
        if (!isDone())
            mSyntherizer = new NonBlockSyntherizer(context.getApplicationContext());
    }

    /**
     * 开始播放
     */
    @Override
    public void play(String text) {
        if (mSyntherizer != null)
            mSyntherizer.speak(text);
    }

    /**
     * 取消合成
     */
    @Override
    public void stopSpeaking() {
        if (mSyntherizer != null)
            mSyntherizer.stop();
    }

    /**
     * 暂停播放
     */
    @Override
    public void pauseSpeaking() {
        if (mSyntherizer != null)
            mSyntherizer.pause();
    }

    /**
     * 继续播放
     */
    @Override
    public void resumeSpeaking() {
        if (mSyntherizer != null)
            mSyntherizer.resume();
    }

    /**
     * 释放资源
     */
    @Override
    public void release() {
        if (mSyntherizer != null)
            mSyntherizer.release();
    }

    /**
     * 获取播放状态
     *
     * @return 0未开始 1开始 2暂停
     */
    @Override
    public int getPlayState() {
        return mSyntherizer != null ? mSyntherizer.getPlayState() : 0;
    }

    @Override
    public void setPlayListener(ITTSListener listener) {
        if (mSyntherizer != null)
            mSyntherizer.setPlayListener(listener);
    }

    /**
     * 是否完成初始化
     */
    public boolean isDone() {
        return mSyntherizer != null && mSyntherizer.isInitDone();
    }

}
